package com.cz.service;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*public class dapingconnect {


    public static void main(String[] args) throws IOException, InterruptedException {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.50\" Port=\"9999\"><CMD type=\"LED_NET_STATUS\" id=\"1\"/></ITS></QDELITS>";
        try {
            //1.建立客户端socket连接，指定服务器位置及端口
            Socket socket =new Socket("192.168.0.79",50100);
            //2.得到socket读写流
            OutputStream os=socket.getOutputStream();
            PrintWriter pw=new PrintWriter(os);
            //输入流
            InputStream is=socket.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            //3.利用流按照一定的操作，对socket进行读写操作
            pw.write(xml);
            pw.flush();
            socket.shutdownOutput();
            //接收服务器的相应
            String reply=null;
            while(!((reply=br.readLine())==null)){
                System.out.println("接收服务器的信息："+reply);
            }
            //4.关闭资源
            br.close();
            is.close();
            pw.close();
            os.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }*/


   public  class Client extends Thread {

        //定义一个Socket对象
        Socket socket = null;

        public Client() {
            try {
                //需要服务器的IP地址和端口号，才能获得正确的Socket对象
                socket = new Socket("192.168.0.79", 50100);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void run() {
            //客户端一连接就可以写数据给服务器了
            new sendMessThread().start();
            super.run();
           try {
                System.out.println(111);
                // 读Sock里面的数据
                InputStream is=socket.getInputStream();
                BufferedReader br=new BufferedReader(new InputStreamReader(is));
                String reply=null;
                while(!((reply=br.readLine())==null)){
                    System.out.println("接收服务器的信息："+reply);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //往Socket里面写数据，需要新开一个线程
        class sendMessThread extends Thread{
            @Override
            public void run() {
                super.run();

                //写操作
                String aa="<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.50\" Port=\"9999\"><PROG id=\"-1\"><ITEMS trad=\"0,25\" wh=\"160,128\"><ITEM type=\"2\" interval=\"65534\"><text color=\"3\" size=\"24\" style=\"0\"  font=\"宋体\"> 车位剩余 大型车位:050小型车位:075</text>" +
                        "</ITEM></ITEMS></PROG></ITS></QDELITS>";
                // String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.50\" Port=\"9999\"><CMD type=\"LED_NET_STATUS\" id=\"1\"/></ITS></QDELITS>";
                OutputStreamWriter os=null;
                PrintWriter pw=null;
                try {
                 os= new OutputStreamWriter(socket.getOutputStream(),StandardCharsets.UTF_8);

                    pw=new PrintWriter(os);
                        pw.write(aa);
                        pw.flush();
                    } catch (IOException ex) {
                    ex.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                   //socket.shutdownOutput();
                   System.err.println(socket);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public static String getdate() {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String result = format.format(date);
            return result;
        }

        //函数入口
        public static void main(String[] args) {
            //需要服务器的正确的IP地址和端口号
            Client clientTest=new Client();
            clientTest.start();
        }

    }



